<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="1600" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false" align-center draggable overflow>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" @click="back">确定</el-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search.purchase_ware_code" placeholder="入库单号" clearable @keyup.enter="upsearch"></el-input>
                    <el-select v-model="search.apply_id" placeholder="供应商" filterable clearable>
                        <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                    </el-select>
                    <el-select v-model="search.input_type" placeholder="订单类型" @change="upsearch" :disabled="select">
                        <el-option label="采购单" :value="1"></el-option>
                        <el-option label="染色单" :value="2"></el-option>
                        <el-option label="加工单" :value="3"></el-option>
                    </el-select>
                    <el-select v-model="search.action_type" placeholder="状态" @change="upsearch" :disabled="select">
                        <el-option label="已使用" :value="1"></el-option>
                        <el-option label="已占用" :value="2"></el-option>
                        <el-option label="已检验" :value="3"></el-option>
                        <el-option label="未检验" :value="4"></el-option>
                    </el-select>
                    <el-select v-model="search.test_complate" placeholder="是否完成" filterable clearable>
                        <el-option label="是" value="1"></el-option>
                        <el-option label="否" value="0"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" @row-dblclick="dbselectionChange" border :params="query" height="700">
                <el-table-column type="selection" width="50" align="center"></el-table-column>
                <el-table-column label="入库类型" prop="purchase_process_code" width="200">
                    <template #default="{row}">
                        <el-tag :type="row.input_type == 1 ? 'primary' : row.input_type == 2 ? 'danger' : row.input_type == 3 ? 'warning' : 'info'" size="small">{{row.input_type == 1 ? '采购单入库' : row.input_type == 2 ? '染色单入库' : row.input_type == 3 ? '加工单入库' : '其他入库'}}</el-tag><br />
                        <el-link :underline="false" type="primary" @click="table_show_order(row)">
                            {{ row.input_type == 1 ? row.purchase?.purchase_code : row.input_type == 2 ? row.dye?.purchase_dye_code :  row.process?.purchase_process_code }}
                        </el-link>
                    </template>
                </el-table-column>
                <el-table-column label="入库单号" prop="purchase_ware_code" width="200">
                    <template #default="{row}">
                        <div style="display: flex;align-items: center;margin: 10px 0;">
                            <el-link :underline="false" type="primary" @click="table_show_purchase(row)">{{ row.purchase_ware_code }}</el-link>
                        </div>
                        <div>{{row.ware_date}}({{row.ware_name}})</div>
                    </template>
                </el-table-column>
                <el-table-column label="供应商" prop="date">
                    <template #default="{row}">
                        <div style="margin: 10px 0;">{{row.apply.apply_name}}</div>
                        <div>{{row.apply.linkman}}</div>
                        <div v-if="row.apply">单价: {{row.purchase?.price}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="染色方">
                    <template #default="{row}">
                        <div style="margin: 10px 0;">{{row.dye_factory?row.dyeFactory.apply_name:''}}</div>
                        <div>{{row.dye_factory?row.dyeFactory.linkman:''}}</div>
                        <div v-if="row.dye_factory">单价: {{row.dye?.price}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="加工方">
                    <template #default="{row}">
                        <div style="margin: 10px 0;">{{row.process_factory?row.processFactory.apply_name:''}}</div>
                        <div>{{row.process_factory?row.processFactory.linkman:''}}</div>
                        <div v-if="row.process_factory">单价: {{row.process?.price}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="品名" prop="goods_name" align="center"></el-table-column>
                <el-table-column label="数量(匹)" prop="total_num" align="center"></el-table-column>
                <el-table-column label="剩余(匹)" prop="can_use_number" align="center"></el-table-column>
                <el-table-column label="克重(克/米)" prop="weight" align="center"></el-table-column>
                <el-table-column label="备注" prop="" align="center"></el-table-column>
                <el-table-column label="业务员" prop="createstaff.name" align="center"></el-table-column>
            </scTable>
        </el-main>
    </el-dialog>
    <!-- 订单 -->
    <purchase-dialog v-if="dialog.purchase" ref="purchaseDialog" @success="handleSuccess" @closed="dialog.purchase = false"></purchase-dialog>
    <dye-dialog v-if="dialog.dye" ref="dyeDialog" @success="handleSuccess" @closed="dialog.dye = false"></dye-dialog>
    <process-dialog v-if="dialog.process" ref="processDialog" @success="handleSuccess" @closed="dialog.process = false"></process-dialog>

    <!-- 入库单 -->
    <purchasehouse-dialog v-if="dialog.purchasehouse" ref="purchasehouseDialog" @success="handleSuccess" @closed="dialog.purchasehouse = false"></purchasehouse-dialog>
    <dyehouse-dialog v-if="dialog.dyehouse" ref="dyehouseDialog" @success="handleSuccess" @closed="dialog.dye = false"></dyehouse-dialog>
    <processhouse-dialog v-if="dialog.processhouse" ref="processhouseDialog" @success="handleSuccess" @closed="dialog.process = false"></processhouse-dialog>
    
</template>

<script>
import purchaseDialog from '@/views/business/purchase/purchase/save.vue'
import dyeDialog from '@/views/business/dye/order/save.vue'
import processDialog from '@/views/business/machining/order/save.vue'


import purchasehouseDialog from '@/views/business/purchase/arrival/save.vue'
import dyehouseDialog from '@/views/business/dye/house/save.vue'
import processhouseDialog from '@/views/business/machining/house/save.vue'
export default {
    props: {
        query: { type: Object, default: () => ({}) },
    },
	emits: ['success', 'closed'],
    components: {
        purchaseDialog, dyeDialog, processDialog, purchasehouseDialog,dyehouseDialog, processhouseDialog
    },
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '入库记录',
                copy: '新增染色入库',
                edit: '编辑染色入库',
                show: '查看染色入库',
            },
            visible: false,
            isSaveing: false,
            dialog: {
                purchase: false,
                dye: false,
                process: false,

                purchasehouse: false,
                dyehouse: false,
                processhouse: false,
            },
            apiObj: this.$API.business.purchase.house.list,
            selection: [],
            search: {
                purchase_ware_code: null,
                apply_id: null,
                action_type: null,
            },
            applyList: [],
        }
    },
    mounted() {
		this.getApply();
    },
    methods: {
        //显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        // 供应商列表
		async getApply() {
			var res = await this.$API.basic.apply.all.get();
			this.applyList = res.data
		},
        // 查看订单
        table_show_order(row){
            let query = {}
            if(row.input_type ==1){
                query = {
                    id: row.purchase_id,
                    purchase_code: row.purchase.purchase_code,
                }
            }else if(row.input_type ==2){
                query = {
                    id: row.dye_id,
                    purchase_dye_code: row.dye.purchase_dye_code,
                }
            }else {
                query = {
                    id: row.process_id,
                    purchase_ware_code: row.process.purchase_process_code,
                }
            }
            this.dialog[row.input_type == 1 ? 'purchase' : row.input_type == 2 ? 'dye' : 'process'] = true
            this.$nextTick(() => {
                this.$refs[row.input_type == 1 ? 'purchaseDialog' : row.input_type == 2 ? 'dyeDialog' : 'processDialog'].open('show').setData(query)
            })
        },
        // 查看入库单
        table_show_purchase(row){
            this.dialog[row.input_type == 1 ? 'purchasehouse' : row.input_type == 2 ? 'dyehouse' : 'processhouse'] = true
            this.$nextTick(() => {
                this.$refs[row.input_type == 1 ? 'purchasehouseDialog' : row.input_type == 2 ? 'dyehouseDialog' : 'processhouseDialog'].open('show').setData(row)
            })
        },
        selectionChange(selection){
            this.selection = selection;
        },
        dbselectionChange(selection){
            this.$emit('success', [selection], this.type)
            this.visible = false;
        },
        back(){
            const firstItem = this.selection[0];   
            const isValid = this.selection.every(item => 
                item.goods_id === firstItem.goods_id && 
                item.apply_id === firstItem.apply_id   
            );
            if (!isValid) {
                this.$message.error("请选择同品名,同供应商的入库单");
                return;
            }
            this.$emit('success', this.selection)
            this.visible = false;
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
    }
}
</script>

<style lang="scss" scoped></style>
